package com.ruoyi.factory.bom.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.factory.bom.domain.BomCost;
import com.ruoyi.factory.bom.domain.MaterialCost;
import com.ruoyi.factory.bom.domain.ProductCost;
import com.ruoyi.factory.bom.domain.UpRecord;
import com.ruoyi.factory.bom.mapper.BomCostMapper;
import com.ruoyi.factory.bom.mapper.MaterialCostMapper;
import com.ruoyi.factory.bom.mapper.ProductCostMapper;
import com.ruoyi.factory.bom.mapper.UpRecordMapper;
import com.ruoyi.factory.bom.service.IUpdateMaterialPriceService;
import com.ruoyi.factory.common.domain.AjaxBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * @ClassName UpdateMaterialPriceServiceImpl
 * @Description: TODO
 * @Author LiuJiaji
 * @Date 2022/11/10 16:54
 **/
@Service
public class UpdateMaterialPriceServiceImpl implements IUpdateMaterialPriceService {

    @Autowired
    private BomCostMapper bomCostMapper;

    @Autowired
    private ProductCostMapper productCostMapper;

    @Autowired
    private MaterialCostMapper materialCostMapper;

    @Autowired
    private UpRecordMapper upRecordMapper;


    @Override
    public AjaxBean upgrtopr(BomCost bomCost){
        BigDecimal ml = bomCost.getGrossProfitRate().divide(new BigDecimal("100"));
        BigDecimal ll = new BigDecimal("1").subtract(ml);
        //成本除以毛利
        BigDecimal divide = bomCost.getTotalFee().divide(ll, 3, BigDecimal.ROUND_HALF_UP);
        BigDecimal divide1 = bomCost.getStTotalFee().divide(ll, 3, BigDecimal.ROUND_HALF_UP);
        bomCost.setStSalePrice(divide1);
        bomCost.setSalePrice(divide);
        ProductCost pc = new ProductCost();
        pc.setPartId(bomCost.getPartId());
        List<ProductCost> productCosts = productCostMapper.selectProductCostList(pc);
        if(!productCosts.isEmpty()){
            ProductCost productCost = productCosts.get(0);
            productCost.setGrossProfitRate(bomCost.getGrossProfitRate());
            productCostMapper.updateProductCost(productCost);
        }
        return new AjaxBean("",true,bomCost);
    }
    /**
     * @author: Liujiaji
     * @date: 2022/11/14 8:38
     * @description: TODO 记录修改信息
     * @param: [type]
     * @return: void
     **/
    @Override
    public void upbomcostinfo(String type, Object obj){

        UpRecord ur = new UpRecord();
        ur.setId(UUID.randomUUID().toString());
        //设置物料的id
        if("0".equals(type)){
            MaterialCost mac = (MaterialCost) obj;
            ur.setPartUuid(mac.getId());
            ur.setType(type);
            ur.setPartId(mac.getPartId());
            ur.setChinaName(mac.getChinaName());
            String jsonObject = JSONObject.toJSONString(mac);
            ur.setParam(jsonObject);
        }else if ("1".equals(type)){
            ProductCost prc = (ProductCost) obj;
            ur.setPartUuid(prc.getId());
            ur.setType(type);
            ur.setPartId(prc.getPartId());
            ur.setChinaName(prc.getChinaName());
            String jsonObject = JSONObject.toJSONString(prc);
            ur.setParam(jsonObject);
        }
        //获取登录用户
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        ur.setUpdateNickname(user.getNickName());
        ur.setUpdateName(user.getUserName());
        //登录ip
        ur.setIpaddr(loginUser.getIpaddr());
        ur.setLoginlocation(loginUser.getLoginLocation());
        ur.setBrowser(loginUser.getBrowser());
        Long loginTime = loginUser.getLoginTime();
        Date date = new Date(loginTime);
        ur.setLogintime(date);
        ur.setUpdateUserId(String.valueOf(loginUser.getUserId()));
        upRecordMapper.insertUpRecord(ur);

    }
    /**
     * @author: Liujiaji
     * @date:  11:23
     * @description: TODO 修改价格后同步价格表
     * @param: [materialCost]
     * @return: void
     **/
    @Override
    public void upbomcostafter(String wuliaoId) {

        BomCost bomCost = new BomCost();
        bomCost.setPartId(wuliaoId);
        List<BomCost> bomCosts = bomCostMapper.selectBomCostList(bomCost);
        //for循环添加root
        Set<String> set = new HashSet<>();
        for (BomCost cost : bomCosts) {
            set.add(cost.getRootCode());
        }
        //遍历root
        for (String s : set) {
            //找到该物料对应的价格等信息进行修改
            BomCost bcp = new BomCost();
            bcp.setPartId(s);
            List<BomCost> bomCosts1 = bomCostMapper.selectBomCostList(bcp);
            BomCost bc = bomCosts1.get(0);
            ProductCost pc = new ProductCost();
            pc.setPartId(s);
            List<ProductCost> productCosts = productCostMapper.selectProductCostList(pc);
            if (!productCosts.isEmpty()) {
                ProductCost productCost = productCosts.get(0);
                //产品
                BigDecimal bigDecimal1 = new BigDecimal("0.00");
                BigDecimal bigDecimal2 = new BigDecimal("0.00");

                if (null != productCost.getWorkFee() && (productCost.getWorkFee().compareTo(new BigDecimal("0")) != 0)) {
                    bigDecimal1 = bigDecimal1.add(productCost.getWorkFee());
                    bc.setWorkFee(productCost.getWorkFee());
                }
                if (null != productCost.getGasFee() && (productCost.getGasFee().compareTo(new BigDecimal("0")) != 0)) {
                    bigDecimal1 = bigDecimal1.add(productCost.getGasFee());
                    bc.setGasFee(productCost.getGasFee());
                }
                if (null != productCost.getElecFee() && (productCost.getElecFee().compareTo(new BigDecimal("0")) != 0)) {
                    bigDecimal1 = bigDecimal1.add(productCost.getElecFee());
                    bc.setElecFee(productCost.getElecFee());
                }
                if (null != productCost.getMakeFee() && (productCost.getMakeFee().compareTo(new BigDecimal("0")) != 0)) {
                    bigDecimal1 = bigDecimal1.add(productCost.getMakeFee());
                    bc.setMakeFee(productCost.getMakeFee());
                }
                if(null != productCost.getAsName()){
                    bc.setAsName(productCost.getAsName());
                }
                Map<String, BigDecimal> stmap = addPrice(bc.getPartId(), bc.getBomVar(), bc.getBomName(), bc.getRootCode());
                BigDecimal st = stmap.get("st");
                BigDecimal te = stmap.get("te");
                bc.setMarktaxPrice(te);
                bc.setTaxPrice(st);
                bigDecimal2 = bigDecimal1;
                bigDecimal1 = bigDecimal1.add(st);
                bc.setStTotalFee(bigDecimal1.multiply(new BigDecimal(bc.getCounts())));
                bigDecimal2 = bigDecimal2.add(te);
                bc.setTotalFee(bigDecimal2.multiply(new BigDecimal(bc.getCounts())));

                BigDecimal ml = bc.getGrossProfitRate().divide(new BigDecimal("100"));
                BigDecimal ll = new BigDecimal("1").subtract(ml);
                //成本除以毛利
                BigDecimal divide = bc.getTotalFee().divide(ll, 3, BigDecimal.ROUND_HALF_UP);
                BigDecimal divide1 = bc.getStTotalFee().divide(ll, 3, BigDecimal.ROUND_HALF_UP);
                bc.setStSalePrice(divide1);
                bc.setSalePrice(divide);
                bomCostMapper.updateBomCost(bc);

            }
        }
    }

    /**
     * @author: Liujiaji
     * @date: 2022/10/26 16:29
     * @description: TODO 循环找价格
     * @param: [partId, counts]
     * @return: java.math.BigDecimal
     **/
    public Map<String,BigDecimal> addPrice (String partId,String bomvar,String bomName, String rootCode) {
        //st为标准总价  te为预测总价
        Map<String,BigDecimal> resultMap= new HashMap<>();
        //标准
        BigDecimal bigDecimal1 = new BigDecimal("0.00");
        //测试
        BigDecimal bigDecimal2 = new BigDecimal("0.00");
        BomCost bd = new BomCost();
        bd.setParentId(partId);
        bd.setBomVar(bomvar);
        bd.setBomName(bomName);

        List<BomCost> bomCosts = bomCostMapper.selectBomCostList(bd);
        for (BomCost bomCost : bomCosts) {
            //判断是不是原料
            MaterialCost mc = new MaterialCost();
            mc.setPartId(bomCost.getPartId());
            List<MaterialCost> materialCosts = materialCostMapper.selectMaterialCostList(mc);
            //如果不是空说明是原料 计算成本
            if (!materialCosts.isEmpty()) {
                //标准
                BigDecimal bigDecimal3 = new BigDecimal("0.00");
                //测试
                BigDecimal bigDecimal4 = new BigDecimal("0.00");
                //含税价*数量 判断到是原料,通过原料找到
//                List<BomData> bomData1 = bomDataMapper.selectBomDataList(bd);
//                BomData bomData2 = bomData1.get(0);
                String counts = bomCost.getCounts();

                MaterialCost materialCost = materialCosts.get(0);
                BigDecimal marktaxPrice = materialCost.getMarktaxPrice();
                BigDecimal marketPrice = materialCost.getMarketPrice();
                BigDecimal taxPrice = materialCost.getTaxPrice();
                //市场价优先
//                BomCost bc = new BomCost();
                if (null != taxPrice && (taxPrice.compareTo(new BigDecimal("0")) != 0)) {
                    bigDecimal3 = taxPrice.multiply(new BigDecimal(counts));
                    bigDecimal4 = taxPrice.multiply(new BigDecimal(counts));
                    bomCost.setTaxPrice(taxPrice);
                    bomCost.setTotalFee(bigDecimal4);
                    bomCost.setStTotalFee(bigDecimal3);
                }
                if (null != marketPrice && (marketPrice.compareTo(new BigDecimal("0")) != 0)) {
                    bomCost.setMarketPrice(marketPrice);
                }
                if (null != marktaxPrice && (marktaxPrice.compareTo(new BigDecimal("0")) != 0)) {
                    bigDecimal4 = marktaxPrice.multiply(new BigDecimal(counts));
                    bomCost.setMarktaxPrice(marktaxPrice);
                    bomCost.setTotalFee(bigDecimal4);
                }
                if(null != materialCost.getAsName()){
                    bomCost.setAsName(materialCost.getAsName());
                }
//                BomCost bc = new BomCost();
//                bc.setId(UUID.randomUUID().toString());
//                bc.setChinaName(bomData.getChinaname());
//                bc.setBomName(bomData.getBomname());
//                bc.setBomVar(bomData.getBomvar());
//                bc.setParentId(bomData.getParentid());
//                bc.setFatherPartvar(bomData.getFatherpartvar());
//                bc.setPartId(bomData.getPartid());
//                bc.setAssembLelevel(bomData.getAssemblelevel());
//                bc.setPartVar(bomData.getPartvar());
//                bc.setCounts(bomData.getCounts());
//                bc.setType("0");
//                bc.setBomName(bomName);
//                bc.setRootCode(rootCode);
                bigDecimal1 = bigDecimal1.add(bigDecimal3);
                bigDecimal2 = bigDecimal2.add(bigDecimal4);
                bomCostMapper.updateBomCost(bomCost);
            }else{
                BigDecimal bigDecimal3 = new BigDecimal("0.00");
                BigDecimal bigDecimal4 = new BigDecimal("0.00");
                //产品计算成本
                ProductCost pc = new ProductCost();
                pc.setPartId(bomCost.getPartId());
                List<ProductCost> productCosts = productCostMapper.selectProductCostList(pc);
                if(!productCosts.isEmpty()){
                    ProductCost productCost = productCosts.get(0);
                    //产品
                    if(null != productCost.getWorkFee() && (productCost.getWorkFee().compareTo(new BigDecimal("0")) !=0)){
                        bigDecimal3 = bigDecimal3.add(productCost.getWorkFee());
                    }
                    if(null != productCost.getGasFee() && (productCost.getGasFee().compareTo(new BigDecimal("0")) !=0)){
                        bigDecimal3 = bigDecimal3.add(productCost.getGasFee());
                    }
                    if(null != productCost.getElecFee() && (productCost.getElecFee().compareTo(new BigDecimal("0")) !=0)){
                        bigDecimal3 = bigDecimal3.add(productCost.getElecFee());
                    }
                    if(null != productCost.getMakeFee() && (productCost.getMakeFee().compareTo(new BigDecimal("0")) !=0)){
                        bigDecimal3 = bigDecimal3.add(productCost.getMakeFee());
                    }
                    Map<String, BigDecimal> stmap = addPrice(productCost.getPartId(), bomvar, bomName, rootCode);
                    BigDecimal st = stmap.get("st");
                    BigDecimal te = stmap.get("te");
                    bigDecimal4 = bigDecimal3.add(te);
                    bigDecimal3 = bigDecimal3.add(st);
                    bomCost.setTaxPrice(bigDecimal3);
                    bomCost.setMarktaxPrice(bigDecimal4);
                    if(null != productCost.getAsName()){
                        bomCost.setAsName(productCost.getAsName());
                    }

//                    BomData bdp = new BomData();
//                    bdp.setPartid(productCost.getPartId());
//                    List<BomData> bomData1 = bomDataMapper.selectBomDataListOrder(bdp);
//                    BomData bomDatum = bomData1.get(0);
//                    BomCost bc = new BomCost();
//                    bc.setId(UUID.randomUUID().toString());
//                    bc.setChinaName(bomData.getChinaname());
//                    bc.setBomName(bomData.getBomname());
//                    bc.setBomVar(bomData.getBomvar());
//                    bc.setParentId(bomData.getParentid());
//                    bc.setFatherPartvar(bomData.getFatherpartvar());
//                    bc.setPartId(bomData.getPartid());
//                    bc.setAssembLelevel(bomData.getAssemblelevel());
//                    bc.setPartVar(bomData.getPartvar());
//                    bc.setCounts(bomData.getCounts());
//                    bc.setType("3");
                    bomCost.setMakeFee(productCost.getMakeFee());
                    bomCost.setGasFee(productCost.getGasFee());
                    bomCost.setElecFee(productCost.getElecFee());
                    bomCost.setWorkFee(productCost.getWorkFee());
                    bigDecimal3 = bigDecimal3.multiply(new BigDecimal(bomCost.getCounts()));
                    bomCost.setStTotalFee(bigDecimal3);
                    bigDecimal4 = bigDecimal4.multiply(new BigDecimal(bomCost.getCounts()));
                    bomCost.setTotalFee(bigDecimal4);
                    bomCostMapper.updateBomCost(bomCost);
                }
            }
        }
        resultMap.put("st",bigDecimal1);
        resultMap.put("te",bigDecimal2);
        return resultMap;
    }
}
